import { reactive } from 'vue'
import { format } from 'date-fns'
import { parseTime } from '@/common/common'
import { Project } from '@/service/modules/project/types'
import { findTablesReq } from '@/service/modules/data-manage'
import { TableInfo } from '@/service/modules/data-manage/types'
import { useRouter } from 'vue-router'

export function useTable() {
  const router = useRouter()
  const state = reactive({
    page: 1,
    limit: 10,
    count: 0,
    searchParam: {
      datasourceId: 0,
      dbName: '',
      searchKey: ''
    },
    list: [],
    loading: false,
    currentRecord: {} as TableInfo,
    modTableRemarkModalShow: false,
    modTableOwnerModalShow: false,
    dropTableModalShow: false
  })

  const getList = async () => {
    if (state.loading) return
    state.loading = true

    const { data, count } = await findTablesReq({
      page: state.page,
      limit: state.limit,
      ...state.searchParam
    })
    state.loading = false
    if (!data) throw Error()
    state.list = data.map((record: Project) => {
      record.createTime = record.createTime
        ? format(parseTime(record.createTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      record.updateTime = record.updateTime
        ? format(parseTime(record.updateTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      return record
    })
    state.count = count
  }

  const updateList = () => {
    if (state.list.length === 1 && state.page > 1) {
      --state.page
    }
    getList()
  }

  const openTableDetailTab = (
    datasourceId: number,
    dbName: string,
    tableName: string
  ) => {
    router.push({
      path: '/dataManage/dataCatalog/tableDetail',
      query: {
        tab:
          window.$t('dataManage.table') +
          `:${tableName}` +
          window.$t('dataManage.tableDetail'),
        tabName: 'TableDetailTab',
        datasourceId: datasourceId,
        dbName: dbName,
        tableName: tableName
      }
    })
  }
  const openTablePrivilegeTab = (tableInfo: TableInfo) => {
    router.push({
      path: '/dataManage/dataCatalog/tablePrivilege',
      query: {
        tab:
          window.$t('dataManage.table') +
          `:${tableInfo.tableName}` +
          window.$t('dataManage.tablePrivilege'),
        tabName: 'TablePrivilegeTab',
        datasourceId: tableInfo.datasourceId,
        dbName: tableInfo.dbName,
        tableName: tableInfo.tableName,
        tableId: tableInfo.id
      }
    })
  }
  const openDataRelatedTab = (
    datasourceId: number,
    dbName: string,
    tableName: string
  ) => {
    router.push({
      path: '/dataManage/dataCatalog/dataRelateds',
      query: {
        tab:
          window.$t('dataManage.table') +
          `:${tableName}` +
          window.$t('dataManage.related'),
        tabName: 'DataRelatedTab',
        datasourceId: datasourceId,
        dbName: dbName,
        tableName: tableName
      }
    })
  }
  const openModTableTab = (
    datasourceId: number,
    dbName: string,
    tableName: string
  ) => {
    router.push({
      path: '/dataManage/dataCatalog/modTable',
      query: {
        tab:
          window.$t('dataManage.table') +
          `:${tableName}` +
          window.$t('common.modify'),
        tabName: 'ModTableTab',
        datasourceId: datasourceId,
        dbName: dbName,
        tableName: tableName
      }
    })
  }
  const onOperationClick = (
    data: { rowData: TableInfo },
    type:
      | 'modTableRemark'
      | 'modTableOwner'
      | 'openTableDetailTab'
      | 'openTablePrivilegeTab'
      | 'openDataRelatedTab'
      | 'openModTableTab'
      | 'dropTable'
  ) => {
    state.currentRecord = data.rowData
    switch (type) {
      case 'modTableRemark':
        state.modTableRemarkModalShow = true
        break
      case 'modTableOwner':
        state.modTableOwnerModalShow = true
        break
      case 'openTableDetailTab':
        openTableDetailTab(
          data.rowData.datasourceId,
          data.rowData.dbName,
          data.rowData.tableName
        )
        break
      case 'openTablePrivilegeTab':
        openTablePrivilegeTab(data.rowData)
        break
      case 'openDataRelatedTab':
        openDataRelatedTab(
          data.rowData.datasourceId,
          data.rowData.dbName,
          data.rowData.tableName
        )
        break
      case 'openModTableTab':
        openModTableTab(
          data.rowData.datasourceId,
          data.rowData.dbName,
          data.rowData.tableName
        )
        break
      case 'dropTable':
        state.dropTableModalShow = true
        break
    }
  }

  const changePage = (page: number) => {
    state.page = page
    getList()
  }

  const changePageSize = (limit: number) => {
    state.page = 1
    state.limit = limit
    getList()
  }

  return {
    state,
    changePage,
    changePageSize,
    updateList,
    onOperationClick
  }
}
